package site.zhanjingbo.shop.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import site.zhanjingbo.shop.meta.Product;
import site.zhanjingbo.shop.meta.User;
import site.zhanjingbo.shop.service.ProductService;
import site.zhanjingbo.shop.utils.Permission;
import site.zhanjingbo.shop.utils.PermissionType;

@Controller
public class IndexController {

	@Resource
	private ProductService productService;

	@RequestMapping("/")
	public ModelAndView index() {
		ModelAndView mav = new ModelAndView();
		List<Product> productList = productService.getProducts();
		mav.addObject("productList", productList);

		mav.setViewName("index");
		return mav;
	}

	@Permission(permission = PermissionType.BUYER)
	@RequestMapping("/index")
	public ModelAndView indexWithUser(int type, HttpServletRequest requeste) {
		ModelAndView mav = new ModelAndView();

		if (type == 0) {
			requeste.getRequestDispatcher("/");
		}
		mav.addObject("listType", type);
		User user = (User) requeste.getSession().getAttribute("user");
		List<Product> productList = productService.getProductsNoUser(user);
		mav.addObject("productList", productList);

		mav.setViewName("index");
		return mav;
	}

}
